package br.com.reparoja.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.reparoja.entities.Brand;
import br.com.reparoja.entities.Equipment;
import br.com.reparoja.persistence.Factory;

public class EquipmentServlet extends HttpServlet {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		try {
			String ret = "";
			String action = req.getParameter("action");

			if (action == null)
				action = "list";

			if (action.equalsIgnoreCase("insert")) {
				insert(req);
				ret = list();
			}
			else if (action.equalsIgnoreCase("delete")) {
				delete(req);
				ret = list();
			}
			else if (action.equalsIgnoreCase("list")) {
				ret = list();
			}

			resp.setContentType("text/plain");
			resp.getWriter().println(ret);
		} catch (Exception e) {
			StringBuilder sb = new StringBuilder();
			sb.append("[");
			
			sb.append("{");
			sb.append("\"id\": ").append(-1).append(", ");
			sb.append("\"name\": \"").append(e.getMessage()).append("\"");
			sb.append("}, ");
			
			StackTraceElement[] stack = e.getStackTrace();
			
			for (StackTraceElement stackTraceElement : stack) {
				sb.append("{");
				sb.append("\"id\": ").append(0).append(", ");
				sb.append("\"name\": \"").append(stackTraceElement.toString()).append("\"");
				sb.append("}, ");
				
			}
			
			sb.setLength(sb.length() - 2);
			sb.append("]");

			resp.getWriter().println(sb.toString());
		}

	}

	private void insert(HttpServletRequest req) {
		EntityManager em = Factory.get().createEntityManager();
		String name = req.getParameter("name");

		if (name != null) {
			Equipment equip = new Equipment();
			equip.setName(req.getParameter("name"));

			em.getTransaction().begin();
			em.persist(equip);
			em.getTransaction().commit();
		}

		em.close();
	}

	private String list() {
		StringBuilder sb = new StringBuilder();
		EntityManager em = Factory.get().createEntityManager();

		TypedQuery<Equipment> consulta = em.createQuery("FROM Equipment", Equipment.class);
		List<Equipment> equipments = consulta.getResultList();

		if (equipments.size() > 0) {
			for (Equipment equipment : equipments) {
				sb.append(equipment.toString()).append(", ");
			}

			sb.setLength(sb.length() - 2);
		}

		em.close();

		sb.insert(0, "[").append("]");

		return sb.toString();
	}

	private void delete(HttpServletRequest req) {
		EntityManager em = Factory.get().createEntityManager();
		String param = req.getParameter("id");

		if (param != null) {
			try {
				Long id = Long.parseLong(param);
				
				Equipment eq = em.find(Equipment.class, id);
				if (eq != null) {
					em.getTransaction().begin();
					em.remove(eq);
					em.getTransaction().commit();
				}		
			} catch (Exception e) {
				// TODO: handle exception
			}
		}

		em.close();
	}


	
}
